<template>
  <div class='sub-sort'>
    <div class="sort">
      <a :class="{active: sortParams.sortField === null}" @click="changeSort()" href="javascript:;">默认排序</a>
      <a :class="{active: sortParams.sortField === 'publishTime'}" @click="changeSort('publishTime')" href="javascript:;">最新商品</a>
      <a :class="{active: sortParams.sortField === 'orderNum'}" @click="changeSort('orderNum')" href="javascript:;">最高人气</a>
      <a :class="{active: sortParams.sortField === 'evaluateNum'}" @click="changeSort('evaluateNum')" href="javascript:;">评论最多</a>
      <a @click="changeSort('price')" href="javascript:;">
        价格排序
        <i class="arrow up" :class="{active: sortParams.sortField === 'price' && sortParams.sortMethods === 'asc'}"></i>
        <i class="arrow down" :class="{active: sortParams.sortField === 'price' && sortParams.sortMethods === 'desc'}"></i>
      </a>
    </div>
    <div class="check">
      <!-- 复选框值改变时触发changeCheck事件 -->
      <ShopCheckbox @change="changeCheck" v-model="sortParams.inventory">仅显示有货商品</ShopCheckbox>
      <ShopCheckbox @change="changeCheck" v-model="sortParams.onlyDiscount">仅显示特惠商品</ShopCheckbox>
    </div>
  </div>
</template>
<script>
import { reactive } from 'vue'
export default {
  name: 'SubSort',
  setup (props, { emit }) {
    // 定义交互的数据
    const sortParams = reactive({
      // 是否显示有货
      inventory: false,
      // 是否显示特惠
      onlyDiscount: false,
      // 排序字段
      sortField: null,
      // 排序顺序
      sortMethods: null
    })
    // 排序函数
    const changeSort = (sortField) => {
      if (sortField === 'price') {
        sortParams.sortField = sortField
        // 处理排序
        // 当第一次点击
        if (sortParams.sortMethods === null) {
          // 为升序
          sortParams.sortMethods = 'desc'
        } else {
          sortParams.sortMethods = sortParams.sortMethods === 'desc' ? 'asc' : 'desc'
        }
      } else {
        // 如果已经选择了该选项，就阻止一直点击
        if (sortParams.sortField === sortField) return
        sortParams.sortField = sortField
        sortParams.sortMethods = null
        console.log(sortParams.sortField)
      }
      // 排序规则改变便会触发
      emit('sort-change', sortParams)
    }
    const changeCheck = () => {
      // 复选框的值改变便会触发
      emit('sort-change', sortParams)
    }
    return { sortParams, changeSort, changeCheck }
  }
}
</script>

<style scoped lang='less'>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: @themeColor;
        border-color: @themeColor;
        color: #fff;
      }
      // 箭头样式
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 6px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
            &.active {
            border-bottom-color: @themeColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @themeColor;
          }
        }
      }
    }
  }
  .check {
    .shop-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
